package mx.netbrain.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import org.apache.log4j.Logger;

import mx.netbrain.business.InstallBusiness;
import mx.netbrain.model.Install;

public class NetBrainUtils {

	private static final Logger logger = Logger.getLogger(NetBrainUtils.class);
	private static Properties prop = new Properties();
	private static InputStream is = null;
	private static char sep = File.separatorChar;

	public static String md5(String password) throws NoSuchAlgorithmException {
		String result = password;
		if (password != null) {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			BigInteger hash = new BigInteger(1, md.digest());
			result = hash.toString(16);
			while (result.length() < 32) {
				result = "0" + result;
			}
		}
		return result;
	}

	public static Properties leerPropertiesConfig() {
		Install install = new Install();
		install = InstallBusiness.findById(1);
		try {
			String ruta = null;
			int n;
			n = install.getProperties().length();
			char car = install.getProperties().charAt(n - 1);
			if (sep != car) {
				ruta = install.getProperties() + sep;
			} else {
				ruta = install.getProperties();
			}
			is = new FileInputStream(install.getEstructure() + ruta
					+ "appConf.properties");
			prop.load(is);
			return prop;
		} catch (Exception e) {
			logger.error("Error en properties: " + e);
			return null;
		}
	}
}
